﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using SisCRA.Classes;
using SisCRA.Database;

namespace SisCRA.Business
{
    public class StudentEnrollmentAvaliationBLL
    {
        #region -Public Methods-

        /// <summary>
        /// Método que chama a camada de dados para inserir ou atualizar uma nota para um aluno em uma atividade de uma disciplina.
        /// </summary>
        /// <param name="studentEnrollmentAvaliation">Objeto Avaliação</param>
        public void SaveStudentEnrollmentAvaliation(StudentEnrollmentAvaliation studentEnrollmentAvaliation)
        {
            try
            {
                StudentEnrollmentAvaliationDAL studentEnrollmentAvaliationDAL = new StudentEnrollmentAvaliationDAL();
                DataTable dtStudentEnrollmentAvaliation = GetStudentEnrollmentAvaliation(studentEnrollmentAvaliation);

                if (dtStudentEnrollmentAvaliation.Rows.Count > 0)
                {
                    studentEnrollmentAvaliationDAL.UpdateStudentDisciplineActivity(studentEnrollmentAvaliation.StudentId, studentEnrollmentAvaliation.DisciplineId, studentEnrollmentAvaliation.ActivityId, studentEnrollmentAvaliation.Grade);
                }
                else
                {
                    studentEnrollmentAvaliationDAL.InsertStudentDisciplineActivity(studentEnrollmentAvaliation.StudentId, studentEnrollmentAvaliation.DisciplineId, studentEnrollmentAvaliation.ActivityId, studentEnrollmentAvaliation.Grade);
                }
                
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Método que verifica se as notas dos alunos em todas as atividades foram cadastradas.
        /// </summary>
        /// <param name="studentEnrollmentAvaliation">Objeto Avaliação</param>
        /// <returns>True se as notas dos alunos em todas as atividades foram cadastradas, caso contrário False</returns>
        public bool IsGradesOk(StudentEnrollmentAvaliation studentEnrollmentAvaliation)
        {
            try
            {
                ActivityDAL activityDAL = new ActivityDAL();
                DataTable dtAtivity = activityDAL.GetAllByDiscipline(studentEnrollmentAvaliation.DisciplineId);

                StudentEnrollmentDAL studentEnrollmentDAL = new StudentEnrollmentDAL();
                DataTable dtStudentEnrollment = studentEnrollmentDAL.GetStudentByDiscipline(studentEnrollmentAvaliation.DisciplineId);

                StudentEnrollmentAvaliationDAL studentEnrollmentAvaliationDAL = new StudentEnrollmentAvaliationDAL();
                DataTable dtStudentEnrollmentAvaliation = studentEnrollmentAvaliationDAL.GetStudentEnrollmentAvaliationByDiscipline(studentEnrollmentAvaliation.DisciplineId);

                if (dtAtivity.Rows.Count * dtStudentEnrollment.Rows.Count == dtStudentEnrollmentAvaliation.Rows.Count)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Método que chama a camada de dados para buscar uma relação Aluno-Disciplina-Atividade.
        /// </summary>
        /// <param name="studentEnrollmentAvaliation">Objeto Avaliação</param>
        /// <returns>DataTable contendo a busca realizada</returns>
        public DataTable GetStudentEnrollmentAvaliation(StudentEnrollmentAvaliation studentEnrollmentAvaliation)
        {
            try
            {
                StudentEnrollmentAvaliationDAL studentEnrollmentAvaliationDAL = new StudentEnrollmentAvaliationDAL();
                return studentEnrollmentAvaliationDAL.GetStudentEnrollmentAvaliation(studentEnrollmentAvaliation.StudentId, studentEnrollmentAvaliation.DisciplineId, studentEnrollmentAvaliation.ActivityId);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Método que chama a camada de dados para buscar as avaliações por aluno e disciplina.
        /// </summary>
        /// <param name="studentEnrollmentAvaliation">Objeto Avaliação</param>
        /// <returns>DataTable contendo a busca realizada</returns>
        public DataTable GetAvaliationByStudentAndDiscipline(StudentEnrollmentAvaliation studentEnrollmentAvaliation)
        {
            try
            {
                StudentEnrollmentAvaliationDAL studentEnrollmentAvaliationDAL = new StudentEnrollmentAvaliationDAL();
                return studentEnrollmentAvaliationDAL.GetAvaliationByDisciplineAndStudent(studentEnrollmentAvaliation.DisciplineId, studentEnrollmentAvaliation.StudentId);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion
    }
}
